<template>
  <!--生产过程信息记录-->
  <basic-dialog v-model="dialog.showDialog" :title="dialog.title" width="1600"
                :autoClose="false" :hideFooter="true">
    <basic-crud  ref="crudRef" :option="options" :autoLoad="true"
                 v-model="formData" :apiListParams="searchData" apiGetListUrl="/mes/produceRecordController/grid"
                 :fastSearch="{}"
                 @row-click="handleRowClick"
                 :row-save="handleAdd"
                 :row-update="handleEdit"
                 :row-del="handleDelete">
      <template #fileNames="{ row }">
        <el-button type="text" v-for="file in row.fileList" :key="file.id"  @click="downloadFileHandler(file)">{{
            `${file.showName}.${file.fileExtension};`
          }}</el-button>
      </template>
      <template #menu-left>
        <avue-upload v-if="!onlyShow" :showFileList="false" style="margin-left: 10px;"
                     :data="extraData"
                     :fileText="'文件上传'"
                     :action="uploadObject.action"
                     :headers="uploadObject.headers"
                     :propsHttp="uploadObject.propsHttp"
                     :upload-after="uploadAfter"
                     :upload-before="uploadBefore"
                     type="upload"
        ></avue-upload>
      </template>
    </basic-crud>
  </basic-dialog>
</template>

<script setup>
import {defineExpose, ref} from "vue";
import { formatDate,formatMilliseconds } from "@/utils/formatTime";
import {downloadFile} from "@/utils/file.js";
import {
  addProcessRecord,
  deleteProcessRecord,
  editProcessRecord
} from "@/apis/mes/problemFeedback";
import {getMpmProcess} from "@/apis/Techne/TechneDeails";
import {useUserStore} from '@/store/user';
import {ElMessage} from "element-plus";
const {token,userInfo} = useUserStore();

const emits = defineEmits(['refresh-parent'])

const dialog = ref({
  showDialog: false,
  title: "生产过程信息记录"
})
const uploadObject = ref({
  propsHttp: {
    res: "obj",
    name: "obj.name",
    url: "fileViewUrl",
  },
  headers: {
    'h-authorization-token': token
  },
  action: '/API/mes/fileController/uploadFile',
});
const parentRow = ref({});
const onlyShow = ref(false);
const searchData=ref({})

// 打开新增编辑对话框
const openDialog = (row, onlyShowValue) => {
  dialog.value.showDialog = true;
  parentRow.value=row;
  onlyShow.value=onlyShowValue
  options.value.addBtn= !onlyShow.value
  options.value.editBtn= !onlyShow.value
  options.value.delBtn= !onlyShow.value
  options.value.cellBtn= !onlyShow.value
  options.value.menu= !onlyShow.value

  searchData.value={
    processTaskId:parentRow.value.id,
    unTotal:true
  }
  //查询工序内容
  getMpmProcess({id: parentRow.value.processId}).then((data) => {
    parentRow.value.processContent = data&& data.length>0 ?data[0].processContent :'';
  });
};

const crudRef=ref(null);
const formData=ref({})
const options= ref({
  addBtn: !onlyShow.value,
  editBtn: !onlyShow.value,
  delBtn: !onlyShow.value,
  align: "center",
  height: "570px",
  menu: !onlyShow.value,
  viewBtn:false,
  cellBtn:true,
  menuWidth: 120,
  column:  [{
    label: '工步序号/工步名称',
    prop: 'code',
    width:180,
    addDisplay:false,
    formatter: () => {
      return parentRow.value.code+"/"+parentRow.value.name;
    }
  },{
    label: '工序内容',
    prop: 'processContent',
    addDisplay:false,
    formatter:() => {
      return parentRow.value.processContent;
    },
    width:600
  }, {
    label: '生产过程信息记录',
    prop: 'recordInfo',
    cell: true,
    type:'textarea',
    span: 24,
  }, {
    label: '备注',
    prop: 'des',
    type:'textarea',
    span: 24,
    cell: true
  }]
})

const currentRow=ref(null);
const extraData=ref({})
const handleRowClick=(row) =>{
  currentRow.value=row
  extraData.value={
    ownId:row.id,
    ownBus:row.bus
  }
}

const uploadBefore=(file, done, loading)=> {
  if (!extraData.value.ownId) {
    ElMessage.error("请先点击行数据选中一条数据！");
    loading();
  } else {
    done();
  }
}
//上传后事件
const uploadAfter=(res, done, loading)=> {
  ElMessage.success("上传成功！");
  crudRef.value.getList();
  loading();
  done();
  extraData.value={}
}
// 新增
const handleAdd=(form, done, loading)=> {
  form.processTaskId = parentRow.value.id;
  form.specificationTaskId = parentRow.value.specificationTaskId;
  form.code = parentRow.value.code;
  form.name = parentRow.value.name;
  addProcessRecord(form).then((res) => {
    ElMessage.success("新增数据成功!");
    refreshHandler();
  }).finally((res) => {
    loading()
    done(form);
  });
}
// 编辑
const handleEdit=(form, index, done, loading)=> {
  form.processTaskId = parentRow.value.id;
  form.specificationTaskId = parentRow.value.specificationTaskId;
  form.code = parentRow.value.code;
  form.name = parentRow.value.name;
  editProcessRecord(form).then((res) => {
    ElMessage.success("修改数据成功!");
    crudRef.value.getList();
    done(form);
  }).finally((res) => {
    loading()
    done(form);
  });
}

// 删除
const handleDelete=(row)=> {
  ElMessageBox.confirm(`此操作将永久删除数据, 是否继续?`, "提示").then(() => {
    deleteProcessRecord(row.id).then((res) => {
      ElMessage.success("删除成功");
      refreshHandler();
    });
  }).catch(() => {
  });
}

//文件下载
const downloadFileHandler=async (row)=> {
  downloadFile(row.id, row.name, "/mes");
}

const refreshHandler=()=> {
  crudRef.value.getList();
  emits('refresh-parent')
}

defineExpose({
  openDialog
})
</script>

<style scoped>

</style>